//
//  HederView.m
//  HuiPinzhijia
//
//  Created by MS on 16/8/30.
//  Copyright © 2016年 LLMPro. All rights reserved.
//

#import "HederView.h"
#import "YYCycleScrollView.h"
#import "HttpService.h"
#import "RequestManager.h"
#import "AdPositionIdModel.h"
#import "MJExtension.h"
#import "UIImageView+WebCache.h"
#define IMGUrl @"http://e.hulianwangdai.com"
#define SCREENW [UIScreen mainScreen].bounds.size.width

@implementation HederView
{
    NSMutableArray * _adArray;
    
    UIScrollView * _scrollView;
    
    YYCycleScrollView * _cycleScrollew;
    UIView *_myView;
    
    BOOL isNetworking;
}

static HederView *_headerView;

+ (HederView *)sharedHederView
{
    if (!_headerView)
    {
        _headerView = [[HederView alloc]init];
    }
    return _headerView;
}
- (void)creatHederView
{
    _myView = [[UIView alloc]initWithFrame:self.frame];
    _myView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_myView];
    
    [self initTopView];
    [self initUI];
    [self footView];
    [self initData];
}
//topView
- (void)initTopView
{
    UIButton *hvBtn = [[UIButton alloc]initWithFrame:CGRectMake(8, 8, 40, 40)];
    [hvBtn setImage:[UIImage imageNamed:@"classify"] forState:UIControlStateNormal];
    [hvBtn addTarget:self action:@selector(JumpToFenlei) forControlEvents:UIControlEventTouchUpInside];
    [_myView addSubview:hvBtn];
    
    UITextField *hvTfd = [[UITextField alloc]initWithFrame:CGRectMake(hvBtn.frame.origin.x + hvBtn.frame.size.width + 5, 8, self.frame.size.width - (hvBtn.frame.origin.x + hvBtn.frame.size.width + 10), 50)];
    hvTfd.placeholder = @"🔍请输入关键字";
    [_myView addSubview:hvTfd];
    
    UIView *view = [[UIView alloc]initWithFrame:hvTfd.frame];
    view.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture)];
    [view addGestureRecognizer:gr];
    [_myView addSubview:view];
    
    
}
//轻叩手势
- (void)tapGesture
{
    if (self.deleagte && [self.deleagte respondsToSelector:@selector(hvTap)])
    {
        [self.deleagte hvTap];
    }
    NSLog(@"轻轻点一下");
}
//btn点击事件
- (void)JumpToFenlei
{
    if (self.deleagte && [self.deleagte respondsToSelector:@selector(hvBtnClick)])
    {
        [self.deleagte hvBtnClick];
    }
    NSLog(@"我被点击了");
}
//滚动视图
- (void)initUI{
    
    //滚动视图
    _cycleScrollew = [[YYCycleScrollView alloc] initWithFrame:CGRectMake(0, 50, self.frame.size.width, self.frame.size.height - 50- 70) animationDuration:3.0];
    
    _cycleScrollew.scrollView.pagingEnabled = YES;
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [_cycleScrollew setTapActionBlock:^(NSInteger(pageIndex)) {
        
        //        NSLog(@"点击的相关的页面%ld",(long)pageIndex);
    }];
    
    [_myView addSubview:_cycleScrollew];
    
 
}
//footView
- (void)footView
{
    UIImageView *footImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, _myView.frame.size.height - 70, self.frame.size.width, 70)];
    footImageView.image = [UIImage imageNamed:@"0.jpg"];
    [_myView addSubview:footImageView];
    
}
#pragma mark -- 初始化数据
- (void)initData{
    
    __weak typeof(self)weak_self = self;
    
    _adArray = [NSMutableArray array];
    
    //判断一下当前网络状态,如果有网,加载网络图片,没有的画加载默认图片(提高用户体验)
    if([HttpService checkNetworkStatus]){
        
        //进行广告数据请求
        [[RequestManager sharedManager] getParameters:nil type:[RequestManager sharedManager].homeIMG success:^(id info) {
            
            NSLog(@"ad_info%@",info);
            
            if ([info[@"status"] integerValue] == 1) {
                
                _adArray = [[NSMutableArray alloc] initWithCapacity:5];
                
                for (int i = 0; i < ((NSArray *)(info[@"data"])).count; i++) {
                    
                    NSLog(@"---imagename= %@",(info[@"data"])[i][@"img"]);
                    
                    NSString * imgurl = [NSString stringWithFormat:@"%@/Public/Upload/Mall/Slide/%@",IMGUrl,(info[@"data"])[i][@"img"]];
                    
                    [_adArray addObject:imgurl];
                    
                    isNetworking = YES;
                }
                
                
            }else{
                
                //加载本地图片到滚动图上
                // [_adArray addObjectsFromArray: ];
                
                isNetworking = NO;
                
            }
            //轮播
            [self showAdWithImageArr:_adArray withController:weak_self isNetWork:isNetworking];
            
            
            
            
        } failure:^(id error) {
            
            //加载本地图片到滚动图上
            // [_adArray addObjectsFromArray: ];
            
           isNetworking = NO;
            
            //[self showAdWithImageArr:_adArray withController:weak_self isNetWork:self.isNetworking];
            
        }];
        
    }else{
        //加载本地图片到滚动图上
        // [_adArray addObjectsFromArray: ];
        
        isNetworking = NO;
        
        //[self showAdWithImageArr:_adArray withController:weak_self isNetWork:self.isNetworking];
    }
    
    
    
}


- (void)showAdWithImageArr:(NSMutableArray *)adArray withController:(HederView *)weak_self isNetWork:(BOOL)isNetWork{
    
    NSMutableArray *ggImageViewArray = [NSMutableArray array];
    
    for (int i = 0; i < adArray.count; i++) {
        
        UIImageView * ggImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, 180)];
        
        if (isNetWork) {
            NSLog(@"图片地址%@",adArray[i]);
            
            [ggImageView sd_setImageWithURL:[NSURL URLWithString:adArray[i]] placeholderImage:[UIImage imageNamed:@"default"] completed:nil];
            
        }else{
            
            
            //ggImageView.image                = [UIImage imageNamed:adArray[i]];
        }
        
        ggImageView.contentMode  = UIViewContentModeScaleAspectFill;
        
        ggImageView.clipsToBounds = YES;
        
        [ggImageViewArray addObject:ggImageView];
        
    }
    
    
    [_cycleScrollew setFetchContentViewAtIndex:^UIView *(NSInteger(pageIndex)) {
        
        
        return [ggImageViewArray objectAtIndex:pageIndex];
        
    }];
    
    [_cycleScrollew setTotalPagesCount:^NSInteger{
        
        return adArray.count;
        
    }]; 
    
    
    
    
}


@end
